const base = require("./base");
module.exports = class extends base {
    async _initialize() {
        await super.isShopAuth();
    }

    /**
     *
     * @api {get} /comment/home/home/list 获取评论列表
     * @apiDescription 获取评论列表
     * @apiGroup comment Home
     * @apiVersion 0.0.1
     *
     * @apiHeader {String} AppToken 应用授权token.
     *
     * @apiParam {Number} page 页码
     *
     * @apiSuccess {Array} success 项目列表
     *
     * @apiSampleRequest /comment/home/home/list
     *
     */
    async list() {
        const shop_id = this.state.shop.id;
        const { page } = this.query;
        const list = await this.model("order_comment")
            .query(qb => {
                qb.where("shop_id", "=", shop_id);
                qb.orderBy("created_at", "desc");
                qb.where("type", "=", 0);
            })
            .fetchPage({
                page,
                withRelated: [
                    "user",
                    "subComment",
                    {
                        subComment: qb => {
                            qb.where("type", 1);
                        }
                    }
                ]
            });
        this.success(list);
    }

    /**
     *
     * @api {post} /comment/home/home/changeStatus 改变评论状态
     * @apiDescription 改变评论状态
     * @apiGroup comment Home
     * @apiVersion 0.0.1
     *
     * @apiHeader {String} AppToken 应用授权token.
     *
     * @apiParam  {Array}  data  [{id:0,status:0}]
     *
     * @apiSampleRequest /comment/home/home/changeStatus
     *
     */
    async changeStatus() {
        const data = this.post;

        await doodoo.bookshelf.Collection.extend({
            model: doodoo.model("order_comment")
        })
            .forge(data)
            .invokeThen("save");

        this.success("评论状态的改变");
    }

    /**
     *
     * @api {post} /comment/home/home/delStatus 删除评论
     * @apiDescription 删除评论
     * @apiGroup comment Home
     * @apiVersion 0.0.1
     *
     * @apiHeader {String} AppToken 应用授权token.
     *
     * @apiParam  {Array} data [{id:0}]
     *
     * @apiSampleRequest /comment/home/home/delStatus
     *
     */

    async delStatus() {
        const data = this.post;
        try {
            await doodoo.bookshelf.Collection.extend({
                model: doodoo.model("order_comment")
            })
                .forge(data)
                .invokeThen("destroy");
        } catch (err) {
            console.log("err-->", err);
        }

        this.success("评论删除成功");
    }

    /**
     *
     * @api {post} /comment/home/home/replyComment 后台评论回复
     * @apiDescription 后台评论回复
     * @apiGroup comment Home
     * @apiVersion 0.0.1
     *
     * @apiHeader {String} AppToken 应用授权token.
     *
     * @apiParam  {number} id
     * @apiParam  {String} content  评论的内容
     *
     * @apiSampleRequest /comment/home/home/replyComment
     *
     */
    async replyComment() {
        const { id, content } = this.post;
        const commentInfo = await this.model("order_comment")
            .query(qb => {
                qb.where("id", "=", id);
            })
            .fetch();
        if (!commentInfo) {
            this.fail("该条评论不存在");
            return;
        }

        // 判断回复是更新还是新增
        const replayInfo = await this.model("order_comment")
            .query(qb => {
                qb.where("pid", "=", id);
                qb.where("type", "=", 1);
            })
            .fetch();
        const data = {
            pid:
                Number(commentInfo.pid) === 0
                    ? commentInfo.id
                    : commentInfo.pid,
            user_id: 0,
            reply_id: commentInfo.user_id,
            content: content,
            type: 1,
            order_id: commentInfo.order_id,
            shop_id: commentInfo.shop_id,
            product_id: commentInfo.product_id,
            status: 1,
            id: replayInfo ? replayInfo.id : 0,
            from_type: replayInfo.from_type
        };

        try {
            await this.model("order_comment")
                .forge(data)
                .save();
        } catch (err) {
            console.log("err-->", err);
        }

        this.success("后台回复成功");
    }
};
